package cn.itsource.pethome.system.controller;

import cn.itsource.pethome.department.domain.Department;
import cn.itsource.pethome.system.domain.Product;
import cn.itsource.pethome.system.query.ProductQuery;
import cn.itsource.pethome.system.service.IProductService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class ProductController {
    @Autowired
        IProductService productService;

    @PostMapping("/pagelist")
    @ApiOperation(value = "高级查询&分页" )
    public PageList<Product> pageQueryAll(@RequestBody ProductQuery productQuery){
        PageList<Product> productPageList = productService.pageQueryAll(productQuery);
        System.out.println(productPageList);
        return productPageList;
    }

    @DeleteMapping("/department/{id}")
    @ApiOperation(value = "根据id删除部门" )
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            productService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("请先删除所有明细！！！");
        }
    }

    @PostMapping("/department")
    @ApiOperation(value = "添加 和更改 部门" )
    public AjaxResult insert(@RequestBody Product product){

        try {
            if(product.getId() == null){
                productService.insert(product);
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
